#include "filter.h"
#include "ui_filter.h"
#include "detail.h"
#include "../utility/connector.h"
#include "../data/company.h"

Filter::Filter(QWidget *parent, Connector *connector) :
    QWidget(parent),connector(connector),
    ui(new Ui::Filter) {
    qDebug()<<"Filter::Filter called.";
    ui->setupUi(this);
    constraint=new QVariantMap;
    companyList=new QList<Company *>;
    //设置当前页面与总的页面，最开始没进行选择，因此当前页面为最初始页面，即为0
    //总的页面数为0
    pageCurr=0;
    pageTotal=0;
    ui->tableWidget_result->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget_result->clearContents();
    ui->tableWidget_result->setRowCount(0);
    ui->label_currentpage->setText(QString::number(pageCurr));
    ui->label_count->setText(QString::number(pageTotal));
    //设置各个按钮的响应
    connect(ui->pushButton_up,SIGNAL(clicked()),
            this,SLOT(pageUp()));
    connect(ui->pushButton_down,SIGNAL(clicked()),
            this,SLOT(pageDown()));
    connect(ui->pushButton_head,SIGNAL(clicked()),
            this,SLOT(pageHead()));
    connect(ui->pushButton_tail,SIGNAL(clicked()),
            this,SLOT(pageTail()));
    connect(ui->spinBox_page,SIGNAL(valueChanged(int)),
            this,SLOT(pageJump(int)));
    connect(ui->tableWidget_result,SIGNAL(cellClicked(int,int)),
            this,SLOT(showDetail(int,int)));
    /* TODO: [Done] 搬砖
     * 绑定所有radioButton的clicked到updateConstraint。
     * 例子如下。
     */
    connect(ui->radioButton_a0,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_a1,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_a2,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_a3,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_a4,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));

    connect(ui->radioButton_b0,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_b1,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_b2,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_b3,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_b4,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));

    connect(ui->radioButton_c0,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_c1,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_c2,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));

    connect(ui->radioButton_d0,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_d1,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_d2,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_d3,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));

    connect(ui->radioButton_e0,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_e1,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_e2,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_e3,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));

    connect(ui->radioButton_f0,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_f1,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_f2,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_f3,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));

    connect(ui->radioButton_g0,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_g1,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_g2,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_g3,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_g4,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));

    connect(ui->radioButton_h0,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_h1,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_h2,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_h3,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_h4,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));

    connect(ui->radioButton_i0,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_i1,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_i2,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_i3,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_i4,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));

    connect(ui->radioButton_j0,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_j1,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_j2,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_j3,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_j4,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));

    connect(ui->radioButton_k0,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_k1,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_k2,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_k3,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_k4,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_k5,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));

    connect(ui->radioButton_l0,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_l1,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_l2,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_l3,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_l4,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_l5,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));

    connect(ui->radioButton_m0,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_m1,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_m2,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_m3,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_m4,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));

    connect(ui->radioButton_n0,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_n1,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_n2,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_n3,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_n4,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    connect(ui->radioButton_n5,SIGNAL(clicked()),
            this,SLOT(updateConstraint()));
    qDebug()<<"Filter::Filter returned.";

}

Filter::~Filter() {
    qDebug()<<"Filter::~Filter called.";
    for(Company *company:*companyList) delete company;
    delete companyList;
    delete ui;
    qDebug()<<"Filter::~Filter returned.";
}

void Filter::filter() {
    qDebug()<<"Filter::filter called.";
    companyList->clear();
    connector->filter(constraint,25,pageCurr,&pageCurr,&pageTotal,companyList);
    /* TODO: [Done] 数据上屏
     * 数据：companyList,pageCurr,pageTotal
     */
    //清空表格，防止上一次结果比此次结果多，导致表格出错。
    ui->tableWidget_result->clearContents();
    for(int i=ui->tableWidget_result->rowCount()-1; i>=0; i--)
        ui->tableWidget_result->removeRow(i);
    //数据上屏
    ui->tableWidget_result->setRowCount(companyList->size());
    QString str;
    for(int i=0; i< companyList->size(); i++) {
        //序号+entname 如果序号未给定
        //str=QString::number(i);
        //ui->tableWidget_result->setItem(i,0,new QTableWidgetItem(str));
        str=*(companyList->value(i)->entname);
        ui->tableWidget_result->setItem(i,0,new QTableWidgetItem(str));
        ui->tableWidget_result->item(i,0)->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget_result->setVerticalHeaderItem(i,new QTableWidgetItem(QString::number(i+(pageCurr-1)*25+1)));

        //tag
        for(int j=0; j<ui->tableWidget_result->columnCount()-1; j++) {
            str=companyList->value(i)->tag->value(j);
            QTableWidgetItem *item=new QTableWidgetItem(str);
            ui->tableWidget_result->setItem(i,1+j,item);
            ui->tableWidget_result->item(i,1+j)->setTextAlignment(Qt::AlignCenter);
        }
        ui->tableWidget_result->setItem(i,ui->tableWidget_result->columnCount()-1,new QTableWidgetItem("点击查看"));
        ui->tableWidget_result->item(i,ui->tableWidget_result->columnCount()-1)->setTextAlignment(Qt::AlignCenter);

    }
    ui->tableWidget_result->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
    ui->tableWidget_result->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
    ui->label_currentpage->setText(QString::number(pageCurr));
    ui->label_count->setText(QString::number(pageTotal));
    qDebug()<<"Filter::filter returned.";
}

void Filter::showDetail(int row, int column) {
    qDebug()<<"Filter::showDetail called.";
    if(companyList->length()<row+1) {
        qDebug()<<"Filter::showDetail Failed.";
        return;
    }
    Detail *detail=new Detail(companyList->at(row));
    detail->show();
    qDebug()<<"Filter::showDetail returned.";
}

void Filter::updateConstraint() {
    qDebug()<<"Filter::updateConstraint called.";
    /* TODO: [Done] 数据下屏
     * 数据：constraint
     */
    //企业类别
    QRadioButton* pbtn1 = qobject_cast<QRadioButton*>(ui->buttonGroup->checkedButton());
    QString name1 = pbtn1->objectName();
    if(!QString::compare(name1, "radioButton_a0")) {
        (*constraint)["企业类别"]="";
    } else {
        (*constraint)["企业类别"]=pbtn1->text();
    }
    //当前状态
    QRadioButton* pbtn2 = qobject_cast<QRadioButton*>(ui->buttonGroup_2->checkedButton());
    QString name2 = pbtn2->objectName();
    if(!QString::compare(name2, "radioButton_b0")) {
        (*constraint)["目前状态"]="";
    }  else if(!QString::compare(name2, "radioButton_b1")) {
        (*constraint)["目前状态"]="在营企业";
    } else if(!QString::compare(name2, "radioButton_b2")) {
        (*constraint)["目前状态"]="注销企业";
    } else if(!QString::compare(name2, "radioButton_b3")) {
        (*constraint)["目前状态"]="吊销企业";
    } else {
        (*constraint)["目前状态"]="迁出企业";
    }
    //驰名商标
    QRadioButton* pbtn3 = qobject_cast<QRadioButton*>(ui->buttonGroup_3->checkedButton());
    QString name3 = pbtn3->objectName();
    if(!QString::compare(name3, "radioButton_d0")) {
        (*constraint)["著名商标"]="";
    } else if(!QString::compare(name3, "radioButton_d1")) {
        (*constraint)["著名商标"]="是（2次入选）";
    } else if(!QString::compare(name3, "radioButton_d2")) {
        (*constraint)["著名商标"]="是";
    } else {
        (*constraint)["著名商标"]=pbtn3->text();
    }
    //著名商标
    QRadioButton* pbtn4 = qobject_cast<QRadioButton*>(ui->buttonGroup_4->checkedButton());
    QString name4 = pbtn4->objectName();
    if(!QString::compare(name4, "radioButton_e0")) {
        (*constraint)["企业技术中小级别"]="";
    } else {
        (*constraint)["企业技术中小级别"]=pbtn4->text();
    }
    //高级技术等级
    QRadioButton* pbtn5 = qobject_cast<QRadioButton*>(ui->buttonGroup_5->checkedButton());
    QString name5 = pbtn5->objectName();
    if(!QString::compare(name5, "radioButton_c0")) {
        (*constraint)["驰名商标"]="";
    } else {
        (*constraint)["驰名商标"]=pbtn5->text();
    }
    //专精特新企业
    QRadioButton* pbtn6 = qobject_cast<QRadioButton*>(ui->buttonGroup_6->checkedButton());
    QString name6 = pbtn6->objectName();
    if(!QString::compare(name6, "radioButton_f0")) {
        (*constraint)["专精特新中小企业"]="";
    } else if(!QString::compare(name6, "radioButton_f1")) {
        (*constraint)["专精特新中小企业"]="是（两次入选）";
    } else if(!QString::compare(name6, "radioButton_f2")) {
        (*constraint)["专精特新中小企业"]="是";
    } else {
        (*constraint)["专精特新中小企业"]=pbtn6->text();
    }
    //信用水平
    QRadioButton* pbtn7 = qobject_cast<QRadioButton*>(ui->buttonGroup_7->checkedButton());
    QString name7 = pbtn7->objectName();
    pbtn7->text();
    if(!QString::compare(name7, "radioButton_g0")) {
        (*constraint)["企业信用-信用水平"]="";
    } else {
        (*constraint)["企业信用-信用水平"]=pbtn7->text();
    }
    //信用危机
    QRadioButton* pbtn8 = qobject_cast<QRadioButton*>(ui->buttonGroup_8->checkedButton());
    QString name8 = pbtn8->objectName();
    if(!QString::compare(name8, "radioButton_h0")) {
        (*constraint)["企业信用-信用危机"]="";
    } else {
        (*constraint)["企业信用-信用危机"]=pbtn8->text();
    }
    //司法风险
    QRadioButton* pbtn9 = qobject_cast<QRadioButton*>(ui->buttonGroup_9->checkedButton());
    QString name9 = pbtn9->objectName();
    if(!QString::compare(name1, "radioButton_i0")) {
        (*constraint)["企业风险-司法风险"]="";
    } else {
        (*constraint)["企业风险-司法风险"]=pbtn9->text();
    }
    //经营风险
    QRadioButton* pbtn10 = qobject_cast<QRadioButton*>(ui->buttonGroup_10->checkedButton());
    QString name10 = pbtn10->objectName();
    if(!QString::compare(name10, "radioButton_j0")) {
        (*constraint)["企业风险-经营风险"]="";
    } else {
        (*constraint)["企业风险-经营风险"]=pbtn10->text();
    }
    //经营能力
    QRadioButton* pbtn11 = qobject_cast<QRadioButton*>(ui->buttonGroup_11->checkedButton());
    QString name11 = pbtn11->objectName();
    if(!QString::compare(name1, "radioButton_k0")) {
        (*constraint)["企业稳定性-企业能力"]="";
    } else {
        (*constraint)["企业稳定性-企业能力"]=pbtn11->text();
    }
    //稳定水平
    QRadioButton* pbtn12 = qobject_cast<QRadioButton*>(ui->buttonGroup_12->checkedButton());
    QString name12 = pbtn12->objectName();
    if(!QString::compare(name1, "radioButton_l0")) {
        (*constraint)["企业稳定性-稳定水平"]="";
    } else {
        (*constraint)["企业稳定性-稳定水平"]=pbtn12->text();
    }
    //企业年龄
    QRadioButton* pbtn13 = qobject_cast<QRadioButton*>(ui->buttonGroup_13->checkedButton());
    QString name13 = pbtn13->objectName();
    if(!QString::compare(name1, "radioButton_m0")) {
        (*constraint)["企业规模背景-企业年龄"]="";
    } else {
        (*constraint)["企业规模背景-企业年龄"]=pbtn13->text();
    }
    //企业规模
    QRadioButton* pbtn14 = qobject_cast<QRadioButton*>(ui->buttonGroup_14->checkedButton());
    QString name14 = pbtn14->objectName();
    if(!QString::compare(name1, "radioButton_n0")) {
        (*constraint)["企业规模背景-企业规模"]="";
    } else {
        (*constraint)["企业规模背景-企业规模"]=pbtn14->text();
    }
    filter();
    qDebug()<<"Filter::updateConstraint returned.";
}

/* TODO: [Done] 翻页业务逻辑
 * 实现以下函数：
 * void pageHead();
 * void pageTail();
 * void pageUp();
 * void pageDown();
 * void pageJump(int);
 * 每个函数都应该是先更新数据成员的值，然后在数据无误的情况下调用filter()。
 */

void Filter::pageHead() {
    qDebug()<<"Filter::pageHead called.";
    if(pageTotal!=0) {
        pageCurr=1;
        filter();
    } else {
        pageCurr=0;
    }
    qDebug()<<"Filter::pageHead returned.";
}

void Filter::pageTail() {
    qDebug()<<"Filter::pageTail called.";
    if(pageTotal!=0) {
        pageCurr=pageTotal;
        filter();
    } else {
        pageCurr=0;
    }
    qDebug()<<"Filter::pageTail returned.";
}

void Filter::pageUp() {
    qDebug()<<"Filter::pageUp called.";
    if(pageTotal!=0) {
        pageCurr--;
        if(pageCurr < 1) {
            pageCurr=1;
        }
        filter();
    } else {
        pageCurr=0;
    }
    qDebug()<<"Filter::pageUp returned.";
}

void Filter::pageDown() {
    qDebug()<<"Filter::pageDown called.";
    if(pageTotal!=0) {
        pageCurr++;
        if(pageCurr > pageTotal) {
            pageCurr=pageTotal;
        }
        filter();
    } else {
        pageCurr=0;
    }
    qDebug()<<"Filter::pageDown returned.";
}

void Filter::pageJump(int page) {
    qDebug()<<"Filter::pageJump called.";
    if(pageTotal!=0) {
        if(page > pageTotal) {
            pageCurr=pageTotal;
        } else if(page < 1) {
            pageCurr=1;
        } else {
            pageCurr=page;
        }
        filter();
    } else {
        pageCurr=0;
    }
    qDebug()<<"Filter::pageJump returned.";
}
